package com.dailywork.basepro.xml;

import com.dailywork.basepro.xml.model.Book;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.util.List;

public class ReadXmlBySAX {

    private static List<Book> books = null;

    private SAXParserFactory sParserFactory = null;
    private SAXParser parser = null;

    public List<Book> getBooks(String fileName) throws Exception {
        SAXParserFactory sParserFactory = SAXParserFactory.newInstance();
        SAXParser parser = sParserFactory.newSAXParser();

        SAXParseHandler handler = new SAXParseHandler();
        parser.parse(fileName, handler);

        return handler.getBooks();
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
            books = new ReadXmlBySAX().getBooks("src/res/books.xml");
            for (Book book : books) {
                System.out.println(book);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
